<template>
  <div class="app-container">
    <div class="fly-card-head">
      <div class="fly-card-head-wrapper">
        <div class="fly-card-head-title"><div class="head-title">群组分类列表</div></div>
      </div>
    </div>
    <div class="fly-card-body">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="分类名称" prop="columnName">
          <el-input
            v-model="queryParams.columnName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索</el-button>
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['group:column:add']"
          >新增</el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="columnList"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column label="分类名称" align="center" prop="columnName" />
        <el-table-column label="群组个数" align="center" prop="countGroup" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['group:column:edit']"
            >修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)" v-hasPermi="['group:column:add']">新增</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['group:column:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加或修改小组分类对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="分类名称" prop="columnName">
            <el-input v-model="form.columnName" placeholder="请输入分类名称" />
          </el-form-item>
          <el-form-item label="父级ID" prop="parentId">
            <treeselect v-model="form.parentId" :options="columnOptions" :normalizer="normalizer" placeholder="请选择父级ID" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listColumn, getColumn, delColumn, addColumn, updateColumn, exportColumn } from "@/api/group/column";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Column",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 小组分类表格数据
      columnList: [],
      // 小组分类树选项
      columnOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        columnName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        columnName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "父级ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询小组分类列表 */
    getList() {
      this.loading = true;
      listColumn(this.queryParams).then(response => {
        this.columnList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换小组分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.columnName,
        children: node.children
      };
    },
	/** 查询部门下拉树结构 */
    getTreeselect() {
      listColumn().then(response => {
        this.columnOptions = [];
        const data = { id: "0", columnName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.columnOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        columnName: undefined,
        parentId: undefined,
        countGroup: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      this.open = true;
      this.title = "添加小组分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.getTreeselect();
      if (row != undefined) {
        this.form.parentId = row.id;
      }
      getColumn(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小组分类";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateColumn(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addColumn(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除小组分类编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delColumn(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>
